package com.moon.synthesis.utils.excel_poi.sheets;

import cn.afterturn.easypoi.entity.vo.NormalExcelConstants;
import cn.afterturn.easypoi.excel.ExcelExportUtil;
import cn.afterturn.easypoi.excel.entity.ExportParams;
import cn.afterturn.easypoi.excel.entity.enmus.ExcelType;
import cn.afterturn.easypoi.excel.export.ExcelExportService;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.streaming.SXSSFWorkbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Collection;
import java.util.Date;
import java.util.List;
import java.util.Map;

public class MyExcelPoiUtils {

    /**
     * 多个sheet 页的 Workbook
     * 单个 ExportView 为一个sheet 页中的数据-》
     *
     * @param type
     * @param moreView
     * @return 多个sheet 页的 Workbook
     */
    public static Workbook getWorkbook(ExcelType type, ExportMoreView moreView) throws Exception {
        List<Map<String, Object>> exportParamList = Lists.newArrayList();
        moreView.getMoreViewList().forEach(exportView -> {
            Map<String, Object> valueMap = Maps.newHashMap();
            valueMap.put(NormalExcelConstants.PARAMS, exportView.getExportParams());
            valueMap.put(NormalExcelConstants.DATA_LIST, exportView.getDataList());
            valueMap.put(NormalExcelConstants.CLASS, exportView.getCls());
            exportParamList.add(valueMap);
        });
        Workbook workbook = getWorkbook(type, 0);
        for (Map map : exportParamList) {
            ExcelExportService excelExportService = new ExcelExportService();
            excelExportService.createSheet(
                    workbook,
                    (ExportParams) map.get(NormalExcelConstants.PARAMS),
                    (Class<?>) map.get(NormalExcelConstants.CLASS),
                    (Collection<?>) map.get(NormalExcelConstants.DATA_LIST));
        }
        return workbook;
    }

    /**
     * 单个sheet 页
     *
     * @param excelTitle
     * @param excelSheet
     * @param pojoClass
     * @param dataSet
     * @return Workbook
     * @throws IOException
     */
    public static Workbook getWorkbook(String excelTitle, String excelSheet, Class<?> pojoClass, Collection<?> dataSet) throws IOException {
        Workbook workbook = ExcelExportUtil.exportExcel(new ExportParams(excelTitle, excelSheet), pojoClass, dataSet);
        return workbook;
    }

    /**
     * 组建一个 Workbook
     *
     * @param type
     * @param size
     * @return
     */
    private static Workbook getWorkbook(ExcelType type, int size) {
        if (ExcelType.HSSF.equals(type)) {
            return new HSSFWorkbook();
        } else {
            return (Workbook) (size < 100000 ? new XSSFWorkbook() : new SXSSFWorkbook());
        }
    }


    /**
     *
     * @param workbook
     * @param originalFilename 全路径
     * @return
     * @throws Exception
     */
    public static String writeWorkbookLocal(Workbook workbook,String originalFilename) throws Exception{
        File savefile = new File(originalFilename);
        if (!savefile.exists()){
            savefile.mkdirs();
        }
        String excelName = "a.xls";
        originalFilename =originalFilename +"\\"+ excelName;
        FileOutputStream fout = new FileOutputStream(originalFilename);
        workbook.write(fout);
        fout.flush();
        fout.close();
        return originalFilename;
    }
}
